package cj.web.yc.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.uc.controller.BaseController;
import cj.web.yc.service.CategoryService;
import cj.web.yc.service.PointConfigService;
import cj.web.yc.service.VideoService;
import cj.web.yc.utils.Constants;

/**
 * 视频查看
 * @author Administrator
 *
 */
@Path("/video")
public class VideoController extends BaseController{
	
	private static final String IS_LOGIN ="IS_LOGIN";//是否需要
	
	private static Logger logger = Logger.getLogger(VideoController.class.getName());
	
	@Autowired
    private CategoryService categoryService;
	@Autowired
    private VideoService videoService;
	@Autowired
    private PointConfigService pointConfigService;
	
	/**
	 * 视频页
	 * @param model
	 * @param categoryId
	 * @return http://localhost/yc-video/video/videoPage?categoryId=6
	 * @throws Exception
	 */
	@Get("videoPage")
	public String videoPage(Model model,@Param("categoryId") int categoryId) throws Exception {
		//类目lis
		List<Map> categorylis = categoryService.queryByPid(categoryId);
		model.add("categorylis", categorylis);
		model.add("categoryId", categoryId);
		return "/views/yc/videoPage.vm";
	}
	
	/**
	 * 类目lis
	 * @param categoryId
	 * @return http://localhost/yc-video/video/categoryId?categoryId=23
	 * @throws Exception
	 */
	@Get("getCategorylis")
	public String getCategorylis(@Param("categoryId") int categoryId) throws Exception {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		//类目lis
		List<Map> categorylis = categoryService.queryByPid(categoryId);
		String categoryIds ="";
		for(Map rep :categorylis){
			categoryIds+=rep.get("id").toString()+",";
		}
		jo.put("categorylis", categorylis);
		jo.put("all", categoryIds.substring(0, categoryIds.length()-1));
		return "@"+jo.toString();
	}
	
	/**
	 * 类目lis——视频列表
	 * @param categoryIds
	 * @param start
	 * @param length
	 * @return http://localhost/yc-video/video/getVideolis?categoryIds=23,25&start=0&length=3
	 * @throws Exception
	 */
	@Get("getVideolis")
	public String getVideolis(@Param("categoryIds") String categoryIds,
			@Param("start") int start,@Param("length") int length) throws Exception {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		Map rep = videoService.query(categoryIds, start, length);
		jo.put("date", rep.get("videolis"));
		jo.put("size", rep.get("size"));
		return "@"+jo.toString();
	}
	
	/**
	 * 视频详情
	 * @param videoId
	 * @return http://localhost/yc-video/video/getVideoDetail?videoId=1
	 * @throws Exception
	 */
	@Get("getVideoDetail")
	public String getVideoDetail(@Param("videoId") int videoId) throws Exception {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		Map rep = videoService.queryVideoDetail(videoId);
		jo.put("videoDetail", rep.get("videoDetail"));
		jo.put("videoDetailOtherlis", rep.get("videoDetailOtherlis"));
		return "@"+jo.toString();
	}
	
	@Get("videoStart")
	public String videoStart(@Param("videoId") int videoId,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> videolis = videoService.getVideoDetail(videoId);
		
		if(videolis!=null && videolis.size()>0){
			//视频类目ID
			int categoryId = Integer.parseInt(videolis.get(0).get("categoryId").toString());
			//视频积分配置视频播放
			List<Map> replis = pointConfigService.queryByCode(IS_LOGIN);
			if(replis!=null && replis.size()>0){
				//积分配置类目组
				String[] vals = replis.get(0).get("categoryId").toString().split(",");
				
				for(int i=0;i<replis.size();i++){
					//判断类目是否相等
					if(categoryId == Integer.parseInt(vals[i])){
						//判断是否登录
						if(isLogin(request)){
							//已登录
							String memberId = getCookieValueByName(request, "memberId");
							Map rep = videoService.videoStart(videoId, Integer.parseInt(memberId));
							jo.put("returnFlag", rep.get("returnFlag"));
							jo.put("returnCode", rep.get("returnCode"));
							return "@"+jo.toString();
						}else{
							//未登录
							jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
							jo.put("returnCode", Constants.returnObj.returnCode.code_3);
							return "@"+jo.toString();
						}
						
					}
				}
			}
		}
		
		return "@"+jo.toString();
	}
	
	@Get("videoEnd")
	public String videoEnd(@Param("videoId") int videoId,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> videolis = videoService.getVideoDetail(videoId);
		
		if(videolis!=null && videolis.size()>0){
			//视频类目ID
			int categoryId = Integer.parseInt(videolis.get(0).get("categoryId").toString());
			//视频积分配置视频播放
			List<Map> replis = pointConfigService.queryByCode(IS_LOGIN);
			if(replis!=null && replis.size()>0){
				//积分配置类目组
				String[] vals = replis.get(0).get("categoryId").toString().split(",");
				
				for(int i=0;i<replis.size();i++){
					//判断类目是否相等
					if(categoryId == Integer.parseInt(vals[i])){
						//判断是否登录
						if(isLogin(request)){
							//已登录
							String memberId = getCookieValueByName(request, "memberId");
							Map rep = videoService.videoEnd(videoId, Integer.parseInt(memberId));
							jo.put("returnFlag", rep.get("returnFlag"));
							jo.put("returnCode", rep.get("returnCode"));
							return "@"+jo.toString();
						}else{
							//未登录
							jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
							jo.put("returnCode", Constants.returnObj.returnCode.code_3);
							return "@"+jo.toString();
						}
						
					}
				}
			}
		}
		
		return "@"+jo.toString();
	}
}